package com.topone.permission.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.topone.Exception.FanZoneException;
import com.topone.permission.entity.Role;
import com.topone.permission.model.UserSeesion;
import com.topone.permission.result.Result;
import com.topone.permission.service.RoleService;
import com.topone.permission.utils.ControllerUtils;

@RestController
@RequestMapping("/role")
public class RoleController {

	@Autowired
	RoleService roleService;
	
	//@Value("#{systermCode}")
	String systermCode="fanzone";
	
	@RequestMapping("/get/role")
	public Result<?> findByUserId(Long userId){
		List<Role> list = roleService.findByUserId(userId);
		return Result.success(list);
	}
	
	@RequestMapping("/get/role/company")
	public Result<?> findByCompanyCodeContainSystemRole(){
		UserSeesion user = ControllerUtils.getUser();
		List<Role> list = roleService.findByCompanyCodeContainSystemRole(user.getCompanyCode(), systermCode);
		return Result.success(list);
	}
	
	@RequestMapping("/create")
	public Result<?> create(@RequestBody Role role){
		UserSeesion user = ControllerUtils.getUser();
		if(StringUtils.isEmpty(role.getCompanyCode())){
			user.setCompanyCode(role.getCompanyCode());
		}else{
			if(!role.equals(role.getCompanyCode())){
				throw  new FanZoneException(5000,"非法操作");
			}
		}
		roleService.save(role);
		return Result.success();
	}
	
	@RequestMapping("/edit")
	public Result<?> edit(@RequestBody Role role){
		UserSeesion user = ControllerUtils.getUser();
		if(StringUtils.isEmpty(role.getCompanyCode())){
			user.setCompanyCode(role.getCompanyCode());
		}else{
			if(!role.equals(role.getCompanyCode())){
				throw  new FanZoneException(5000,"非法操作");
			}
		}
		roleService.edit(role, role.getCompanyCode());
		return Result.success();
	}
	
}
